﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;
using Joyintech.Common;

namespace Joyintech.Admin.inpiao365.general.createhtml
{
    /// <summary>
    /// 生成文件页面
    /// </summary>
    public partial class createFile : System.Web.UI.Page
    {
        #region 页面初始化

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //加载单页模板
                DataBindFileTemplate();
            }
        }

        #endregion

        #region 加载单页模板

        /// <summary>
        /// 加载单页模板
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindFileTemplate()
        {
            List<systemTemplate> list = new List<systemTemplate>();

            list = new systemTemplate_BLL().GetModelList("TemplateType='FileTemplate' order by TemplateId");

            if (list != null && list.Count > 0)
            {
                foreach (systemTemplate model in list)
                {
                    ListItem item = new ListItem(model.TemplateName, model.TemplateId.ToString());

                    this.listFileTemplate.Items.Add(item);
                }
            }
        }

        #endregion

        #region 页面按钮事件

        /// <summary>
        /// 生成文件页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreateFile_Click(object sender, EventArgs e)
        {
            string selectedNodeId = string.Empty;

            foreach (ListItem item in this.listFileTemplate.Items)
            {
                if (item.Selected)
                {
                    selectedNodeId += item.Value + ",";
                }
            }

            if (selectedNodeId == "")
            {
                MessageBox.Show(this, "没有选择需要的文件页！");
                return;
            }
            else
            {
                selectedNodeId = Utils.ClearLastChar(selectedNodeId);

                List<systemTemplate> list = new systemTemplate_BLL().GetModelList("TemplateId in (" + selectedNodeId + ") order by TemplateId");

                foreach (systemTemplate model in list)
                {
                    //生成文件页
                    VelocityHelper vh = new VelocityHelper();
                    vh.Init(@"~/templates/file/");

                    string bllName = ConfigHelper.GetConfigString("BLL");
                    System.Reflection.Assembly BLL = System.Reflection.Assembly.Load(bllName);   

                    Type[] types = BLL.GetTypes();

			        // 遍历程序集中的类
                    foreach (Type type in types)
                    {
                        string className = type.ToString().Replace(bllName + ".", "");

                        object stu = type.Assembly.CreateInstance(type.ToString());

                        //添加页面操作类BLL
                        vh.Put(className, stu);
                    }

                    string html = vh.Display(model.RelatedFileName);

                    string directory = model.FileCreatePath;

                    //判断栏目页面文件夹是否存在，如果不存在则创建
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    //文件页面名称，默认只有一页
                    string fileName = directory + model.CreatedFileFullName + model.CreatedFileExtName;

                    StreamWriter objSw = new StreamWriter(fileName, false, Encoding.GetEncoding(model.TemplateCharset));
                    objSw.WriteLine(html);
                    objSw.Flush();
                    objSw.Close();
                }
            }
            MessageBox.Show(this, "生成成功！");
        }

        /// <summary>
        /// 删除内容页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteFile_Click(object sender, EventArgs e)
        {
            string selectedNodeId = string.Empty;

            foreach (ListItem item in this.listFileTemplate.Items)
            {
                if (item.Selected)
                {
                    selectedNodeId += item.Value + ",";
                }
            }

            if (selectedNodeId == "")
            {
                MessageBox.Show(this, "没有选择需要的文件页！");
                return;
            }
            else
            {
                selectedNodeId = Utils.ClearLastChar(selectedNodeId);

                List<systemTemplate> list = new systemTemplate_BLL().GetModelList("TemplateId in (" + selectedNodeId + ") order by TemplateId");

                foreach (systemTemplate model in list)
                {
                    string directory = model.FileCreatePath;

                    //判断栏目页面文件夹是否存在，如果不存在则创建
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    //文件页面名称，默认只有一页
                    string fileName = directory + model.CreatedFileFullName + model.CreatedFileExtName;

                    File.Delete(fileName);
                }
            }

            MessageBox.Show(this, "删除成功！");
        }

        #endregion
    }
}